package com.joshua.viewtest.demo.cardview;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.TextView;

import com.joshua.viewtest.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class CardViewTest2 extends AppCompatActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list)
    RecyclerView recyclerView;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.appbar)
    AppBarLayout appBar;

    private ArrayList<Person> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_view_test2);
        ButterKnife.bind(this);
        initData();

        setSupportActionBar(toolbar);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new RecyclerAdapter());
        recyclerView.setOnScrollListener(new HidingScrollListener() {
            @Override
            public void onHide() {
                hideViews();
            }

            @Override
            public void onShow() {
                showViews();
            }
        });
    }

    private boolean visible = true;

    private void hideViews() {
        visible = false;
        recyclerView.setClipToPadding(false);
        appBar.animate().translationY(-appBar.getHeight()).setInterpolator(new
                AccelerateInterpolator(2));
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab
                .getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        fab.animate().translationY(fab.getHeight() + fabBottomMargin).setInterpolator(new
                AccelerateInterpolator(2)).start();
    }

    Handler mHandler = new Handler();

    private void showViews() {
        visible = true;
        appBar.animate().translationY(0).setInterpolator(new AccelerateInterpolator(2))
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (visible)
                                    recyclerView.setClipToPadding(true);
                            }
                        });
                    }
                });
        fab.animate().translationY(0).setInterpolator(new AccelerateInterpolator(2));
    }

    private void initData() {
        list.add(new Person(R.drawable.yt, "瑛太", "日本男演员", " - 出生于日本东京都板桥区莲根"));
        list.add(new Person(R.drawable.jhl, "锦户亮", "日本歌手、演员", " - 出生于兵库县尼崎市"));
        list.add(new Person(R.drawable.rgjt, "染谷将太", "日本演员、导演、编剧", " - 出生于日本东京江东区"));
        list.add(new Person(R.drawable.yt, "瑛太", "日本男演员", " - 出生于日本东京都板桥区莲根"));
        list.add(new Person(R.drawable.jhl, "锦户亮", "日本歌手、演员", " - 出生于兵库县尼崎市"));
        list.add(new Person(R.drawable.rgjt, "染谷将太", "日本演员、导演、编剧", " - 出生于日本东京江东区"));
        list.add(new Person(R.drawable.yt, "瑛太", "日本男演员", " - 出生于日本东京都板桥区莲根"));
        list.add(new Person(R.drawable.jhl, "锦户亮", "日本歌手、演员", " - 出生于兵库县尼崎市"));
        list.add(new Person(R.drawable.rgjt, "染谷将太", "日本演员、导演、编剧", " - 出生于日本东京江东区"));
        list.add(new Person(R.drawable.yt, "瑛太", "日本男演员", " - 出生于日本东京都板桥区莲根"));
        list.add(new Person(R.drawable.jhl, "锦户亮", "日本歌手、演员", " - 出生于兵库县尼崎市"));
        list.add(new Person(R.drawable.rgjt, "染谷将太", "日本演员、导演、编剧", " - 出生于日本东京江东区"));
        list.add(new Person(R.drawable.yt, "瑛太", "日本男演员", " - 出生于日本东京都板桥区莲根"));
        list.add(new Person(R.drawable.jhl, "锦户亮", "日本歌手、演员", " - 出生于兵库县尼崎市"));
        list.add(new Person(R.drawable.rgjt, "染谷将太", "日本演员、导演、编剧", " - 出生于日本东京江东区"));
    }

    class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(CardViewTest2.this)
                    .inflate(R.layout.item_recylcler_card_view, parent, false));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.face.setImageResource(list.get(position).face);
            holder.name.setText(list.get(position).name);
            holder.occupation.setText(list.get(position).occupation);
            holder.description.setText(list.get(position).description);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.face_image)
            CircleImageView face;
            @BindView(R.id.name)
            TextView name;
            @BindView(R.id.occupation)
            TextView occupation;
            @BindView(R.id.description)
            TextView description;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }

    class Person {
        int face;
        String name;
        String occupation;
        String description;

        public Person(int face, String name, String occupation, String description) {
            this.face = face;
            this.name = name;
            this.description = description;
            this.occupation = occupation;
        }
    }

    private static final String TAG = "joshua";

    abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
        private static final int HIDE_THRESHOLD = 20;
        private int scrollDistance = 0;
        private boolean visible = true;

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            boolean isInTop = false;
            View firstChild = recyclerView.getChildAt(0);
            int topChildIndex = recyclerView.getChildAdapterPosition(firstChild);
            int firstChildTop = firstChild.getTop();
            if (topChildIndex == 0) {
                if (firstChildTop > -120 && firstChildTop < 120) {
                    isInTop = true;
                }
            }
            if (scrollDistance > HIDE_THRESHOLD && visible) {
                if (!isInTop) {
                    onHide();
                    visible = false;
                }
                scrollDistance = 0;
            } else if (scrollDistance < -HIDE_THRESHOLD && !visible) {
                onShow();
                visible = true;
                scrollDistance = 0;
            }

            if ((visible && dy > 0) || (!visible && dy < 0)) {
                scrollDistance += dy;
            }
        }

        public abstract void onHide();

        public abstract void onShow();
    }
}
